﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VisualAgile.Controllers;
using System.Web.Mvc;
using System.Web.Routing;
using VisualAgile.DTO;
using VisualAgile.DTO.Mappers;
using System.Web.Security;
using System.Security.Principal;
using System.Web;
using VisualAgile.Web.Tests.Fakes;
using System.Collections;
using ProjectRouting.Controllers;

namespace VisualAgile.Web.Tests
{
    /// <summary>
    /// Summary description for ReleaseControllerTests
    /// </summary>
    [TestClass]
    public class ReleaseControllerTests : BaseControllerTests
    {
        public static ReleaseController GetController()
        {
            ReleaseController controller = new ReleaseController();
            ControllerContext controllerContext = new ControllerContext(new MockHttpContext(), new RouteData(), controller);
            controller.ControllerContext = controllerContext;
            return controller;
        }        

        [TestMethod]
        public void IndexAction_Should_Return_Releases()
        {
            // Arrange
            var controller = GetController();

            // Act
            var result = (JsonResult)controller.Index(new Guid("29DC7A46-52B5-4941-AAE2-F48592847E42"));

            // Results should be ResultDTO
            ResultDTO actionData = result.Data as ResultDTO;
            Assert.IsNotNull(actionData, "No results found");

            // Result should be true            
            Assert.IsTrue(actionData.Result, actionData.Message);

            // Assert
            Assert.AreEqual("Success", actionData.Message);

            //user data should not be null
            Assert.IsNotNull(actionData.Data, "No Data found");
        }

        [TestMethod]
        public void IndexAction_Should_Create_And_Return_Releases()
        {
            // Arrange
            var controller = GetController();

            // Act
            var result = (JsonResult)controller.Index(Guid.NewGuid());

            // Results should be ResultDTO
            ResultDTO actionData = result.Data as ResultDTO;
            Assert.IsNotNull(actionData, "No results found");

            // Result should be true            
            Assert.IsTrue(actionData.Result, actionData.Message);

            // Assert
            Assert.AreEqual("Success", actionData.Message);

            //user data should not be null
            Assert.IsNotNull(actionData.Data, "No Data found");
        }

        [TestMethod]
        public void UpdateAction_Should_Create_And_Return_New_Release()
        {
            // Arrange
            var controller = GetController();

            // Act
            var result = (JsonResult)controller.Update(ReleaseDTOMapper.MapToDTO(FakeReleaseData.DefaultRelease1()), false);

            // Results should be ResultDTO
            ResultDTO actionData = result.Data as ResultDTO;
            Assert.IsNotNull(actionData, "No results found");

            // Result should be true            
            Assert.IsTrue(actionData.Result, actionData.Message);

            // Assert
            Assert.AreEqual("Success", actionData.Message);

            //user data should not be null
            Assert.IsNotNull(actionData.Data, "No Data found");
        }

        [TestMethod]
        public void UpdateAction_Should_Update_And_Return_Release()
        {
            // Arrange
            var controller = GetController();

            var release = FakeReleaseData.DemoRelease();
            release.Name = "Internal Release";

            // Act
            var result = (JsonResult)controller.Update(ReleaseDTOMapper.MapToDTO(release), true);

            // Results should be ResultDTO
            ResultDTO actionData = result.Data as ResultDTO;
            Assert.IsNotNull(actionData, "No results found");

            // Result should be true            
            Assert.IsTrue(actionData.Result, actionData.Message);

            // Assert
            Assert.AreEqual("Success", actionData.Message);

            //user data should not be null
            Assert.IsNotNull(actionData.Data, "No Data found");
        }

        [TestMethod]
        public void DeleteAction_Should_Delete_Release()
        {
            // Arrange
            var controller = GetController();            

            // Act
            var result = (JsonResult)controller.Delete(new Guid("CEA1BCED-DDD4-4480-BF1D-D4B759AC536E"));

            // Results should be ResultDTO
            ResultDTO actionData = result.Data as ResultDTO;
            Assert.IsNotNull(actionData, "No results found");

            // Result should be true            
            Assert.IsTrue(actionData.Result, actionData.Message);

            // Assert
            Assert.AreEqual("Success", actionData.Message);            
        }

        [TestMethod]
        public void DeleteAction_Should_Not_Delete()
        {
            // Arrange
            var controller = GetController();

            // Act
            var result = (JsonResult)controller.Delete(new Guid("E4EB0656-8685-439e-B4EA-66D5C9BA7508"));

            // Results should be ResultDTO
            ResultDTO actionData = result.Data as ResultDTO;
            Assert.IsNotNull(actionData, "No results found");

            // Result should be true            
            Assert.IsFalse(actionData.Result, actionData.Message);           
        }
      
    }
}
